<?php

if (!defined('APP')) {
    die('error');
}



class Excel extends ApiBase
{

    //构造函数
    function __construct()
    {
        parent::__construct();
        $this->getDb();
    }

    // 下载批次订单
    function downloadPiCi()
    {
        $pici = $this->db->getval('pici'); 
        $query = "select * from " . QZ . "dingdan where  pici='{$pici}' order by fee desc , shijian desc";
        $result = $this->db->exec($query);
        //下载数据
        $this->downloadXLS($result,'excel_'. $pici .'_'. time(), false);
    }

    //下载表格 mysql查询结果，文件名，直接下载/保存
    function downloadXLS($result, $fileName, $tf)
    {
        $i = 0;
        $titleArray = [];
        $dataArray = [];
        //echo '<pre>';
        while ($row = $this->db->result_getrow($result) ) 
        {
            //var_dump($row);
            $rowArray = [];
            foreach ($row as $k => $v) {
                if ($i == 0) {
                    if (!is_numeric($k)) {
                        $titleArray[] = $k;
                    }
                }
                if (is_numeric($k)) {
                    $rowArray[] = $v;
                }
            }
            $i += 1;
            $dataArray[] = $rowArray;
        }
        //echo '</pre>';

        $this->exportExcel(
            $titleArray,
            $dataArray,
            $fileName,
            'download/',
            $tf
        );
    }



    /** 
     * 数据导出 
     * 来源网址
     * https://blog.csdn.net/a8240357/article/details/80758222
     * @param array $title   标题行名称 
     * @param array $data   导出数据 
     * @param string $fileName 文件名 
     * @param string $savePath 保存路径 
     * @param $type   是否下载  false--保存   true--下载 
     * 
     * @return string   返回文件全路径 
     * 
     * @throws PHPExcel_Exception 
     * @throws PHPExcel_Reader_Exception 
     */
    function exportExcel(
        $title = array(),
        $data = array(),
        $fileName = 'filename',
        $savePath = 'upload/',
        $isDown = false
    ) {
    	require_once "PHPExcel18/Classes/PHPExcel.php";
        $obj = new PHPExcel();
        //横向单元格标识  
        $cellName = array(
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 
            'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI','AJ', 
            'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 
            'AS',  'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'
        );
        //设置sheet名称  
        $obj->getActiveSheet(0)->setTitle('pici');
        //设置纵向单元格标识 
        $_row = 1;
        if ($title) {
            $_cnt = count($title);
            //合并单元格
            $obj->getActiveSheet(0)->mergeCells('A' . $_row . ':' . $cellName[$_cnt - 1] . $_row);
            //设置合并后的单元格内容
            $obj->setActiveSheetIndex(0)->setCellValue('A' . $_row, '数据导出：' . date('Y-m-d H:i:s'));
            $_row++;
            $i = 0;
            foreach ($title as $v) {
                //设置列标题  
                $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i] . $_row, $v);
                $i++;
            }
            $_row++;
        }
        //填写数据  
        if ($data) {
            $i = 0;
            foreach ($data as $_v) {
                $j = 0;
                foreach ($_v as $_cell) {
                    $obj->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + $_row), $_cell);
                    $j++;
                }
                $i++;
            }
        }

        //文件名处理  
        if (!$fileName) {
            $fileName = uniqid(time(), true);
        }

        $objWrite = PHPExcel_IOFactory::createWriter($obj, 'Excel2007');

        if ($isDown) {
            //网页下载  
            header('pragma:public');
            header("Content-Disposition:attachment;filename={$fileName}.xls");
            $objWrite->save('php://output');
            exit();
           
        }

        $_fileName = iconv("utf-8", "gb2312", $fileName);   //转码  
        $_savePath = $savePath . $_fileName . '.xlsx';
        $objWrite->save($_savePath);
        return $savePath . $fileName . '.xlsx';
    }
	
}
